package javacource.redis.subscribe;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * Create by chenhd on 2021/10/23.
 */
@Component
public class RedisSubscribe implements MessageListener {
    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = redisTemplate.getStringSerializer().deserialize(message.getChannel());
        String msg = redisTemplate.getStringSerializer().deserialize(message.getBody());
        System.out.println(String.format("收到消息主题[%s]的消息:[%s].", channel, msg));
    }
}
